package net.tv90.volunteerserveradmin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import net.tv90.volunteerserveradmin.annotation.OptLogger;
import net.tv90.volunteerserveradmin.constant.OptLoggerConstant;
import net.tv90.volunteerserveradmin.constant.RoleConstant;
import net.tv90.volunteerserveradmin.model.dto.QueryDTO;
import net.tv90.volunteerserveradmin.model.dto.VolunteerProjectDTO;
import net.tv90.volunteerserveradmin.model.dto.VolunteerProjectQueryDTO;
import net.tv90.volunteerserveradmin.model.vo.VolunteerProjectVO;
import net.tv90.volunteerserveradmin.service.ProjectTeacherRelationService;
import net.tv90.volunteerserveradmin.service.VolunteerProjectRelationService;
import net.tv90.volunteerserveradmin.service.VolunteerProjectService;
import net.tv90.volunteerserveradmin.utils.entity.PageResult;
import net.tv90.volunteerserveradmin.utils.entity.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Validated
@CrossOrigin
@RestController
@RequestMapping("volunteerProject")
@Tag(name = "志愿者项目管理接口")
public class VolunteerProjectController {

    @Resource
    private VolunteerProjectService volunteerProjectService;
    @Resource
    private ProjectTeacherRelationService projectTeacherRelationService;
    @Resource
    private VolunteerProjectRelationService volunteerProjectRelationService;

    @GetMapping("/all")
    @SaCheckLogin
    @OptLogger(OptLoggerConstant.QUERY)
    @Operation(summary = "获取所有志愿者项目", description = "获取所有志愿者项目的简要信息")
    public Result<List<VolunteerProjectVO>> getAllVolunteerProjects() {
        return Result.success(volunteerProjectService.getAllVolunteerProjects());
    }

    @GetMapping("/{id}")
    @SaCheckLogin
    @OptLogger(OptLoggerConstant.QUERY)
    @Operation(summary = "获取志愿者项目信息", description = "根据项目ID获取项目的详细信息")
    public Result<VolunteerProjectVO> getVolunteerProjectById(@PathVariable Long id) {
        return Result.success(volunteerProjectService.getVolunteerProjectById(id));
    }

    @PostMapping("/query")
    @SaCheckLogin
    @OptLogger(OptLoggerConstant.QUERY)
    @Operation(summary = "查询志愿者项目信息", description = "复杂查询获取志愿者项目详细信息")
    public Result<PageResult<VolunteerProjectVO>> queryVolunteerProjects(@RequestBody VolunteerProjectQueryDTO dto) {
        return Result.success(volunteerProjectService.queryVolunteerProject(dto));
    }

    @PostMapping("/add")
    @SaCheckRole(RoleConstant.TEACHER)
    @OptLogger(OptLoggerConstant.ADD)
    @Operation(summary = "添加志愿者项目", description = "添加新的志愿者项目信息")
    public Result<Void> addVolunteerProject(@RequestPart(value = "volunteerProjectDTO") VolunteerProjectDTO volunteerProjectDTO,
                                            @RequestPart(value = "file", required = false) MultipartFile file
    ) {
        volunteerProjectService.addVolunteerProject(volunteerProjectDTO, file);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    @SaCheckRole(RoleConstant.TEACHER)
    @OptLogger(OptLoggerConstant.UPDATE)
    @Operation(summary = "更新志愿者项目信息", description = "根据项目ID更新项目的详细信息")
    public Result<Void> updateVolunteerProject(@PathVariable Long id,
                                               @RequestPart(value = "volunteerProjectDTO") VolunteerProjectDTO volunteerProjectDTO,
                                               @RequestPart(value = "file", required = false) MultipartFile file
    ) {
        volunteerProjectService.updateVolunteerProject(id, volunteerProjectDTO, file);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckRole(RoleConstant.TEACHER)
    @OptLogger(OptLoggerConstant.DELETE)
    @Operation(summary = "删除志愿者项目", description = "根据项目ID删除项目")
    public Result<Void> deleteVolunteerProject(@PathVariable Long id) {
        volunteerProjectService.deleteVolunteerProject(id);
        return Result.success();
    }

    @PostMapping("/{studentId}/select-project/{projectId}")
    @SaCheckRole(RoleConstant.STUDENT)
    @OptLogger(OptLoggerConstant.ADD)
    @Operation(summary = "学生选志愿者项目", description = "学生选择志愿者项目并参与")
    public Result<Void> selectVolunteerProject(@PathVariable Long studentId, @PathVariable Long projectId) {
        volunteerProjectRelationService.addStudentToProject(studentId, projectId);
        return Result.success();
    }

    @PostMapping("/query/student/{studentId}/projects")
    @SaCheckLogin
    @OptLogger(OptLoggerConstant.QUERY)
    @Operation(summary = "查询某学生参与的志愿者项目", description = "根据学生ID查询其参与的志愿者项目详细信息，支持分页")
    public Result<PageResult<VolunteerProjectVO>> getStudentVolunteerProjects(@PathVariable Long studentId, @RequestBody QueryDTO dto) {
        return Result.success(volunteerProjectService.getVolunteerProjectsByStudentId(studentId, dto));
    }

    @PostMapping("/query/teacher/{teacherId}/projects")
    @SaCheckLogin
    @OptLogger(OptLoggerConstant.QUERY)
    @Operation(summary = "查询某教师参与的志愿者项目", description = "根据教师ID查询其负责的志愿者项目详细信息，支持分页")
    public Result<PageResult<VolunteerProjectVO>> getTeacherVolunteerProjects(@PathVariable Long teacherId, @RequestBody QueryDTO dto) {
        return Result.success(volunteerProjectService.getVolunteerProjectsByTeacherId(teacherId, dto));
    }

    @PostMapping("/{studentId}/quit-project/{projectId}")
    @SaCheckRole(RoleConstant.STUDENT)
    @OptLogger(OptLoggerConstant.DELETE)
    @Operation(summary = "学生退选志愿者项目", description = "学生根据学生id和志愿项目id退选志愿者项目")
    public Result<Void> quitVolunteerProject(@PathVariable Long studentId, @PathVariable Long projectId) {
        volunteerProjectRelationService.removeStudentFromProject(studentId, projectId);
        return Result.success();
    }


    @PostMapping("/add/{teacherId}/project/{projectId}")
    @SaCheckRole(RoleConstant.ADMIN)
    @OptLogger(OptLoggerConstant.ADD)
    @Operation(summary = "为项目添加教师", description = "管理员为指定项目添加负责的教师")
    public Result<Void> addTeacherToProject(@PathVariable Long teacherId, @PathVariable Long projectId) {
        projectTeacherRelationService.addProjectToTeacher(teacherId, projectId);
        return Result.success();
    }

    @DeleteMapping("/remove/{teacherId}/project/{projectId}")
    @SaCheckRole(RoleConstant.ADMIN)
    @OptLogger(OptLoggerConstant.DELETE)
    @Operation(summary = "从项目中移除教师", description = "管理员从指定项目中移除负责的教师")
    public Result<Void> removeTeacherFromProject(@PathVariable Long teacherId, @PathVariable Long projectId) {
        projectTeacherRelationService.removeProjectFromTeacher(teacherId, projectId);
        return Result.success();
    }

    @PostMapping("/select/{teacherId}/project/{projectId}")
    @SaCheckRole(RoleConstant.TEACHER)
    @OptLogger(OptLoggerConstant.ADD)
    @Operation(summary = "教师选择志愿活动项目", description = "教师根据教师id和志愿项目id选择志愿者项目")
    public Result<Void> teacherSelectProject(@PathVariable Long teacherId, @PathVariable Long projectId) {
        projectTeacherRelationService.teacherSelectProject(teacherId, projectId);
        return Result.success();
    }

    @DeleteMapping("/quit/{teacherId}/project/{projectId}")
    @SaCheckRole(RoleConstant.TEACHER)
    @OptLogger(OptLoggerConstant.DELETE)
    @Operation(summary = "教师退选志愿活动项目", description = "教师根据教师id和志愿项目id退选志愿者项目")
    public Result<Void> teacherQuitProject(@PathVariable Long teacherId, @PathVariable Long projectId) {
        projectTeacherRelationService.teacherQuitProject(teacherId, projectId);
        return Result.success();
    }

}
